using System;
using System.Linq;
using Coderoom.ScrumPlanner.Data.Repositories;
using Coderoom.ScrumPlanner.Domain;

namespace Coderoom.ScrumPlanner.Tests.FakeRepositories
{
	public sealed class FakeUserStoryRepository : UserStoryRepository
	{
		private UserStoryCollection _data;

		public FakeUserStoryRepository()
		{
			SetupFakeData();
		}

		public override UserStoryCollection GetUserStories(Guid themeId)
		{
			var returnData = _data.Where(theme => theme.ThemeId == themeId);

			var themes = new UserStoryCollection();
			foreach (var theme in returnData)
				themes.Add(theme);

			return themes;
		}

		public override UserStory GetUserStory(Guid userStoryId)
		{
			return _data.Where(userStory => userStory.Id == userStoryId).SingleOrDefault();
		}

		public override void CreateUserStory(UserStory userStory)
		{
			userStory.Id = Guid.NewGuid();

			_data.Add(userStory);
		}

		public override bool DeleteUserStory(Guid userStoryId)
		{
			UserStory userStory;

			if ((userStory = GetUserStory(userStoryId)) != null)
				return _data.Remove(userStory);

			return false;
		}

		private void SetupFakeData()
		{
			_data = new UserStoryCollection();

			_data.Add(new UserStory {Id = new Guid("5DB514B6-BFDC-44BC-AD75-7AF8D3BF0D71"), ThemeId = new Guid("AEA523B9-DE70-4660-BD78-2741BFCCFCCA"), Title = "UserStory A"});
			_data.Add(new UserStory {Id = new Guid("A5960D03-0648-4D17-BF55-8C0E86DDEC32"), ThemeId = new Guid("AEA523B9-DE70-4660-BD78-2741BFCCFCCA"), Title = "UserStory B"});
			_data.Add(new UserStory {Id = new Guid("A8FF5DAF-4A94-4DCE-8E5F-24F03D6F6A2B"), ThemeId = new Guid("21FA55C7-CCD5-48D7-B5A9-BB9FD3CE1582"), Title = "UserStory X"});

			foreach (var theme in _data)
				theme.Slug = Entity.CreateSlug(theme.Title);
		}
	}
}